package com.dabai.applicationmanager.transfer;
 
import android.os.AsyncTask;
import android.util.Log;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * Created by shuangyue on 2016/9/22 9:46.
 */
 
public class DataAsyncTask extends AsyncTask<Void,Void,String> {
 
    @Override
    protected void onPreExecute () {
        super.onPreExecute ();
    }
 
    @Override
    protected String doInBackground (Void... voids) {
        String str = null;
        try {
            ServerSocket serverSocket = new ServerSocket (8866);
            //等待客户端来连接
            Socket client = serverSocket.accept ();
 
            InputStream is = client.getInputStream ();
            ByteArrayOutputStream baos = new ByteArrayOutputStream ();
            int i = 0;
            while((i = is.read ())!=-1){
                baos.write (i);
            }
            str = baos.toString ();
            serverSocket.close ();
 
        } catch (IOException e) {
            e.printStackTrace ();
        }
 
        return str;
    }
 
    @Override
    protected void onProgressUpdate (Void... values) {
        super.onProgressUpdate (values);
    }
 
    @Override
    protected void onPostExecute (String s) {
        super.onPostExecute (s);
        Log.d ("DBTAG", "onPostExecute: "+s);
    }
}